package yelmor.comm100.util;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.StringReader;

/**
 * Created by yel on 2016/5/5.
 */
public class XmppMessageParser {

  Document document;

  DocumentBuilderFactory factory;
  DocumentBuilder builder;
  StringBuilder xmlStringBuilder;

  public XmppMessageParser(String xmlStanza) throws ParserConfigurationException {
    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();
    xmlStringBuilder = new StringBuilder();


    String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><a><b></b><c></c></a>";


  }

  public static String getBodyText(String xmlStanza) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try
    {
      builder = factory.newDocumentBuilder();
      Document document = builder.parse( new InputSource( new StringReader( xmlStanza) ) );
      Node body = document.getElementsByTagName("body").item(0);
      return body.getTextContent();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}
